﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

namespace ExcellOnServices.Models.Utils
{
	public class ContextHelper
	{
		public static bool IsPropertyExists(object srcObject, string propertyName) {
			if (srcObject == null) {
				throw new System.ArgumentNullException("srcObject");
			}

			if ((propertyName == null) || (propertyName == String.Empty) || (propertyName.Length == 0)) {
				throw new System.ArgumentException("Property name cannot be empty or null.");
			}

			PropertyInfo propInfoSrcObj = srcObject.GetType().GetProperty(propertyName);

			return (propInfoSrcObj != null);
		}

		public static bool SetValue(object obj, string propertyName, object propertyValue) {
			Type type = obj.GetType();

			PropertyInfo prop = type.GetProperty(propertyName);

			if (null != prop && prop.CanWrite) {
				prop.SetValue(obj, propertyValue, null);
				return true;
			}

			return false;
		}
	}

}